/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package image.edge;

import image.processing.ImageProcessor;
import java.awt.LinearGradientPaint;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

/**
 *
 * @author tommy
 */
public class RobertsEdgeDetector extends EdgeDetectorImpl {
	
	public RobertsEdgeDetector (BufferedImage src) {
		super(src);
	}
	
	public BufferedImage FindEdges() {
		int [][] gradientMagnitudes;
		double gdx, gdy;
		
		WritableRaster outRaster;
		Raster inRaster;

		// Only intensity (grey-scale) images supported for the time being
		if (src.getType() != BufferedImage.TYPE_BYTE_GRAY) {
			return null;
		}
		
		// Output image does not retain padded border
		gradientMagnitudes = new int[(src.getHeight()-2)][(src.getWidth()-2)];
		
		// get original image pixels
		inRaster = src.getData();
		
		// Applies Roberts Operator to find gradient levels for each intensity pixel!
		// Starts at index 1 to height - 2, to avoid processing the external 1 pixel
		// padding added in the constructor of the class
		for (int i = 1 ; i < src.getHeight() -1 ; i++) {
			for (int j = 1 ; j < src.getWidth() -1 ; j++) {
				gdx = inRaster.getSample(j, i, 0) - inRaster.getSample(j+1,i+1,  0);
				gdy = inRaster.getSample(j+1,i,0) - inRaster.getSample(j,i+1,0);
				gradientMagnitudes[i-1][j-1] = (int)Math.ceil(Math.sqrt(Math.pow(gdx, 2) + Math.pow(gdy, 2)));
			}
		}
		
		outRaster = ret.getRaster();
		gradientMagnitudes = ImageProcessor.LinearRangeCompressionChannel(gradientMagnitudes);
		// builds image based on roberts gradient magnitude
		for (int y = 0 ; y < ret.getHeight() ; y++) {
			for (int x = 0 ; x < ret.getWidth() ; x++) {
				outRaster.setSample(x, y, 0, gradientMagnitudes[y][x]);
			}
		}
		return ret;
	}
	
}
